package com.example.testapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testapplication.R;
import com.example.testapplication.activity.HomeActivity;
import com.example.testapplication.api.Api;
import com.example.testapplication.api.ApiConfig;
import com.example.testapplication.api.CallBack;
import com.example.testapplication.entity.Result;
import com.example.testapplication.entity.ShoppingMsgEntity;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

public class ShoppingDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<ShoppingMsgEntity> listDatas;

    public void setListDatas(List<ShoppingMsgEntity> listDatas) {
        this.listDatas = listDatas;
    }

    public ShoppingDataAdapter(Context context) {
        this.mContext = context;
    }
    public ShoppingDataAdapter(Context context, List<ShoppingMsgEntity> datas) {
        this.listDatas=datas;
        this.mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_shopping,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        ShoppingMsgEntity shoppingMsgEntity = listDatas.get(position);
        vh.tvName.setText(shoppingMsgEntity.getName());
        vh.tvPrice.setText(String.valueOf(shoppingMsgEntity.getPrice()));
        vh.tvDescription.setText(shoppingMsgEntity.getDescription());
        vh.tvInventory.setText(String.valueOf(shoppingMsgEntity.getInventory()));
        vh.tvSalenum.setText(String.valueOf(shoppingMsgEntity.getSalenum()));

        vh.btnAddCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Long goodId = shoppingMsgEntity.getId();
                addcart(goodId);
            }
        });
    }

    private void addcart(Long goodId) {
        HashMap<String,Object> params = new HashMap<String,Object>();
        params.put("goodId", goodId);
        Api.config(ApiConfig.USER_ADDCART,params).postRequest(mContext,new CallBack() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Result res = gson.fromJson(result, Result.class);
                if(res.getCode() == 1){

                }else{

                }
            }

            @Override
            public void onFailure(IOException e) {

            }
        });
    }

    @Override
    public int getItemCount() {
        if(listDatas!=null&&listDatas.size()>0)
        {
            return listDatas.size();
        }else{
            return 0;
        }

    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvName;
        private TextView tvPrice;
        private TextView tvDescription;
        private TextView tvInventory;
        private TextView tvSalenum;
        private Button btnAddCart;
        public ViewHolder(@NonNull View view) {
            super(view);
            tvName = view.findViewById(R.id.shopping_name);
            tvPrice = view.findViewById(R.id.shopping_price);
            tvDescription = view.findViewById(R.id.shopping_description);
            tvInventory = view.findViewById(R.id.shopping_inventory);
            tvSalenum = view.findViewById(R.id.shopping_salenum);
            btnAddCart = view.findViewById(R.id.add_cart);
        }
    }
}
